using Gateway.Common.Enums;
using Gateway.Common.Models;
using Gateway.Domain.Entities;
using Gateway.IApplication.Project;
using Gateway.IApplication.Project.Input;
using Gateway.IRepositories;

namespace Gateway.Application;
/// <summary>
/// 
/// </summary>
public class ProjectApp : IProjectApp
{
    private readonly IAppRepository<Project> _repository;
    private readonly IUnitOfWork _unitOfWork;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="repository"></param>
    /// <param name="unitOfWork"></param>
    public ProjectApp(IAppRepository<Project> repository, IUnitOfWork unitOfWork)
    {
        _repository = repository;
        _unitOfWork = unitOfWork;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// <exception cref="MyException"></exception>
    public async Task<bool> Add(CreateProjectInput input)
    {
        var check = await _repository.FirstOrDefaultAsync(p =>
            p.ProjectCode.Equals(input.ProjectCode) && p.Deleted == BooleanStatusEnum.No);
        if (check != null)
        {
            throw new MyException("已存在此项目编码的项目");
        }

        var entity = new Project(projectName: input.ProjectName, projectCode: input.ProjectCode, seqNo: input.SeqNo);
        await _repository.AddAsync(entity);
        await _unitOfWork.CommitAsync();
        return true;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// <exception cref="MyException"></exception>
    public async Task<bool> Update(UpdateProjectInput input)
    {
        var check = await _repository.FirstOrDefaultAsync(p =>
            p.ProjectCode.Equals(input.ProjectCode) && p.Deleted == BooleanStatusEnum.No &&
            !p.Id.Equals(input.ProjectId));
        if (check != null)
        {
            throw new MyException("已存在此项目编码的项目");
        }

        var entity = await _repository.GetAsync(input.ProjectId);
        if (entity == null)
        {
            throw new MyException("数据无效");
        }

        entity.Update(projectName: input.ProjectName, projectCode: input.ProjectCode, seqNo: input.SeqNo);
        await _repository.UpdateAsync(entity);
        await _unitOfWork.CommitAsync();
        return true;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <param name="disable"></param>
    /// <returns></returns>
    /// <exception cref="MyException"></exception>
    public async Task<bool> Disable(string id, string disable)
    {
        var entity = await _repository.GetAsync(id);
        if (entity == null)
        {
            throw new MyException("数据无效");
        }

        entity.SetDisabled(disable);
        await _repository.UpdateAsync(entity);
        await _unitOfWork.CommitAsync();
        return true;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    /// <exception cref="MyException"></exception>
    public async Task<bool> Remove(string id)
    {
        var entity = await _repository.GetAsync(id);
        if (entity == null)
        {
            throw new MyException("数据无效");
        }

        entity.SetDeleted();
        await _repository.UpdateAsync(entity);
        await _unitOfWork.CommitAsync();
        return true;
    }
}